package com.zqxq.lottery.controller.order;

import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.common.constant.HuaYangConstant;
import com.zqxq.lottery.entity.bo.order.UserOrderBO;
import com.zqxq.lottery.entity.po.order.UserOrderPO;
import com.zqxq.lottery.entity.po.sys.LotteryHnumPO;
import com.zqxq.lottery.mapper.sys.LotteryHnumMapper;
import com.zqxq.lottery.mq.Sender;
import com.zqxq.lottery.service.order.IBonusCalcService;
import com.zqxq.lottery.service.order.IUserOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 说    明：  奖金计算
 * 创 建 人：	一刀·胡
 * 创建时间：	2018/7/2 9:20
 * 修 改 人：
 * 修改日期：
 **/
@RestController
@Slf4j
public class BonusCalcController extends OrderBaseController{

    @Autowired
    private IBonusCalcService bonusCalcService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private LotteryHnumMapper lotteryHnumMapper;
    @Autowired
    private Sender sender;
    @Autowired
    private IUserOrderService userOrderService;


    //竞猜计算
    @PostMapping("/bonus/calc")
    public void lotteryBonusCalc(Long lotteryId) {
        if(lotteryId == 21L) {
            updateCalcFootballAllUserOrder();
        }else if(lotteryId == 210L){
            updateCalcGuessChampionAllUserOrder();
        }else if(lotteryId == 10L){
            updateCalcCompetitionAllUserOrder();
        }else if(lotteryId == 24L){
            updateCalcBasketballAllUserOrder();
        }else {
            LotteryHnumPO lotteryNum = lotteryHnumMapper.getTopOpenForCalc(lotteryId);
            if (null != lotteryNum && 0 == lotteryNum.getCalc_status() && (lotteryNum.getStatus() == 3 || lotteryNum.getStatus() == 4)) {
                updateCalcAllUserOrder(lotteryNum);
            }
        }
    }
    @PostMapping("/bonus/calc2")
    public void lotteryBonusCalc2(Long lotteryId) {
        lotteryBonusCalc(11L);
        sender.send(HuaYangConstant.MQ_OPENPRIZE_BET_FC,"12",1000*60*1);
    }

    private void updateCalcFootballAllUserOrder(){
        String key = "CALC_JOB_21";
        String value = stringRedisTemplate.opsForValue().get(key);
        /*if (StrUtils.isNotEmpty(value))
            return;*/
        try {
            stringRedisTemplate.opsForValue().set(key, key);
            Long max = bonusCalcService.getMaxOrderId21();
            List<UserOrderPO> list = bonusCalcService.query21OrderForCal(null,max);
            while (list.size()>0){
                bonusCalcService.updateCalcUserOrders21(list);
                Long start = list.get(list.size()-1).getId();
                list = bonusCalcService.query21OrderForCal(start,max);
            }
        } catch (Exception e) {
            log.error("计算失败：" + e);
        } finally {
            stringRedisTemplate.delete(key);
        }
    }
    private void updateCalcBasketballAllUserOrder(){
        String key = "CALC_JOB_24";
        String value = stringRedisTemplate.opsForValue().get(key);
        if (StrUtils.isNotEmpty(value))
            return;
        try {
            stringRedisTemplate.opsForValue().set(key, key);
            Long max = bonusCalcService.getMaxOrderId24();
            List<UserOrderPO> list = bonusCalcService.query24OrderForCal(null,max);
            while (list.size()>0){
                bonusCalcService.updateCalcUserOrders24(list);
                Long start = list.get(list.size()-1).getId();
                list = bonusCalcService.query24OrderForCal(start,max);
            }
        } catch (Exception e) {
            log.error("计算失败：" + e);
        } finally {
            stringRedisTemplate.delete(key);
        }
    }
    private void updateCalcGuessChampionAllUserOrder(){
        String key = "CALC_JOB_210";
        String value = stringRedisTemplate.opsForValue().get(key);
        if (StrUtils.isNotEmpty(value))
            return;
        try {
            stringRedisTemplate.opsForValue().set(key, key);
            Long max = bonusCalcService.getMaxOrderId210();
            List<UserOrderPO> list = bonusCalcService.query210OrderForCal(null,max);
            while (list.size()>0){
                bonusCalcService.updateCalcUserOrders210(list);   //开奖算法
                Long start = list.get(list.size()-1).getId();
                list = bonusCalcService.query210OrderForCal(start,max);
            }
        } catch (Exception e) {
            log.error("计算失败：" + e);
        } finally {
            stringRedisTemplate.delete(key);
        }
    }
    private void updateCalcCompetitionAllUserOrder(){
        String key = "CALC_JOB_10";
        String value = stringRedisTemplate.opsForValue().get(key);
        if (StrUtils.isNotEmpty(value))
            return;
        try {
            stringRedisTemplate.opsForValue().set(key, key);
            Long max = bonusCalcService.getMaxOrderId10();
            List<UserOrderPO> list = bonusCalcService.query10OrderForCal(null,max);
            while (list.size()>0){
                bonusCalcService.updateCalcUserOrders10(list);   //开奖算法
                Long start = list.get(list.size()-1).getId();
                list = bonusCalcService.query10OrderForCal(start,max);
            }
        } catch (Exception e) {
            log.error("计算失败：" + e);
        } finally {
            stringRedisTemplate.delete(key);
        }
    }

    private void updateCalcAllUserOrder(LotteryHnumPO lotteryNum){
        String key = "CALC_JOB_" + lotteryNum.getLottery_id() + "_" + lotteryNum.getPeriod();
        String value = stringRedisTemplate.opsForValue().get(key);
        if (StrUtils.isNotEmpty(value))
            return;
        try {
            log.info("开始计算：" + key);
            stringRedisTemplate.opsForValue().set(key, key);
            List<UserOrderPO> list = bonusCalcService.queryDigitOrderForCal(lotteryNum.getLottery_id(),lotteryNum.getPeriod());
            while (list.size()>0){
                bonusCalcService.updateCalcUserOrders(list,lotteryNum);
                list = bonusCalcService.queryDigitOrderForCal(lotteryNum.getLottery_id(),lotteryNum.getPeriod());
            }
            lotteryNum.setCalc_status(1);
            lotteryHnumMapper.updateById(lotteryNum);
        } catch (Exception e) {
            log.error("计算失败：" + e);
        } finally {
            stringRedisTemplate.delete(key);
        }
    }

    //竞猜兑奖
    @PostMapping("/bonus/prize")
    public void lotteryBonusPrize(Long lotteryId) {
        String key = "BONUS_PRIZE_JOB";
        String value = stringRedisTemplate.opsForValue().get(key);
        if (StrUtils.isNotEmpty(value))
            return;
        try {
            stringRedisTemplate.opsForValue().set(key, key);
            List<UserOrderPO> list = bonusCalcService.queryOrderForPrize(lotteryId);
            for (UserOrderPO order : list)
                bonusCalcService.updateOrderForPrize(order);
            /*while (list.size()>0){
                for (UserOrderPO order : list)
                    bonusCalcService.updateOrderForPrize(order);
                list = bonusCalcService.queryOrderForPrize(lotteryId);
            }*/
        } catch (Exception e) {
            log.error("兑奖失败：" + e);
        } finally {
            stringRedisTemplate.delete(key);
            log.info(key);
        }
    }
    @PostMapping("/batchRefund")
    public void batchRefund() {
        String key = "BATCH_REFUND_JOB";
        String value = stringRedisTemplate.opsForValue().get(key);
        if (StrUtils.isNotEmpty(value))
            return;
        try {
            stringRedisTemplate.opsForValue().set(key, key);
            List<UserOrderPO> list = bonusCalcService.queryNotOpenPrizeOrder();
            for (UserOrderPO order : list){
                //订单退款
                userOrderService.backMoneyCancelOrder(String.valueOf(order.getId()));
            }
        } catch (Exception e) {
            log.error("失败：" + e);
        } finally {
            stringRedisTemplate.delete(key);
            log.info(key);
        }
    }

}
